package com.hnust.controller;

import com.hnust.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@Controller
@CrossOrigin("*")
@RequestMapping("/download")
public class DowloadController {

	/*
	 * @Author Tonited
	 * @Description //TODO 获取学生的模板
	 * @Date 19:29 2020/10/30
	 * @Param [request, response]
	 * @return void
	 **/
	@RequestMapping("student")
	public Result<String> getStudentTemple(HttpServletRequest request , HttpServletResponse response) throws IOException {
		try {
			getFile(request, response, "批量上传学生模板", "xlsx");
		}catch (Exception e){
			return new Result(0,e.getMessage(),null);
		}
		return new Result(1,"上传成功",null);
	}
	/*
	 * @Author Tonited
	 * @Description //TODO 获取教师的模板
	 * @Date 19:29 2020/10/30
	 * @Param [request, response]
	 * @return void
	 **/
	@RequestMapping("teacher")
	public Result<String> getTeacherTemple(HttpServletRequest request , HttpServletResponse response) throws IOException {
		try{
			getFile(request,response,"批量上传教师模板","xlsx");
		}catch (Exception e){
			return new Result(0,e.getMessage(),null);
		}
		return new Result(1,"上传成功",null);
	}

	/*
	 * @Author Tonited
	 * @Description //TODO 获取导入题目的模板
	 * @Date 19:29 2020/10/30
	 * @Param [request, response]
	 * @return void
	 **/
	@RequestMapping("question")
	public Result<String> getQuestionTemple(HttpServletRequest request , HttpServletResponse response) throws IOException {
		try{
			getFile(request,response,"批量导入题目模板","docx");
		}catch (Exception e){
			return new Result(0,e.getMessage(),null);
		}
		return new Result(1,"上传成功",null);
	}

	/*
	 * @Author Tonited
	 * @Description //TODO 获取课程目标模板
	 * @Date 19:31 2020/10/30
	 * @Param [request, response]
	 * @return void
	 **/
	@RequestMapping("coutar")
	public Result<String> getCourseTargetTemple(HttpServletRequest request , HttpServletResponse response) throws IOException {
		try{
			getFile(request,response,"批量导入课程目标模板","docx");
		}catch (Exception e){
			return new Result(0,e.getMessage(),null);
		}
		return new Result(1,"上传成功",null);
	}
	
	/*
	 * @Author Tonited
	 * @Description //TODO 获取专业毕业要求分解指标点模板
	 * @Date 19:32 2020/10/30
	 * @Param [request, response]
	 * @return void
	 **/
	@RequestMapping("graduate")
	public Result<String> getGraduateTemple(HttpServletRequest request , HttpServletResponse response) throws IOException {
		try{
			getFile(request,response,"专业毕业要求分解指标点模板","docx");
		}catch (Exception e){
			return new Result(0,e.getMessage(),null);
		}
		return new Result(1,"上传成功",null);
	}

	//获取文件的工具方法
	//type: docx, xlsx,png.jpg
	private void getFile(HttpServletRequest request , HttpServletResponse response, String fileName,String type) throws IOException {
		fileName+="."+type;
		String filePath = System.getProperty("user.dir")+"/assets/example/"+fileName;
		response.setCharacterEncoding("UTF-8");
		fileName = URLEncoder.encode(fileName,"UTF-8");
		response.setHeader("Content-disposition", "attachment; filename="+fileName);
		//根据路径获取文件
		File docFile = new File(filePath);
		//获取文件后缀名格式
		String ext = docFile.getName().substring(docFile.getName().indexOf("."));
		//判断图片格式,设置相应的输出文件格式
		if(ext.equals("jpg")){
			response.setContentType("image/jpeg");
		}else if(ext.equals("JPG")){
			response.setContentType("image/jpeg");
		}else if(ext.equals("png")){
			response.setContentType("image/png");
		}else if(ext.equals("PNG")){
			response.setContentType("image/png");
		}else if(ext.toLowerCase().equals("doc")){
			response.setContentType("application/msword");
		}else if(ext.toLowerCase().equals("docx")){
			response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
		}else if(ext.toLowerCase().equals("xls")){
			response.setContentType("application/vnd.ms-excel application/x-excel ");
		}else if(ext.toLowerCase().equals("xlsx")){
			response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		}

		//读取指定路径下面的文件
		InputStream in = new FileInputStream(docFile);
		OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
		//创建存放文件内容的数组
		byte[] buff =new byte[1024];
		//所读取的内容使用n来接收
		int n;
		//当没有读取完时,继续读取,循环
		while((n=in.read(buff))!=-1){
			//将字节数组的数据全部写入到输出流中
			outputStream.write(buff,0,n);
		}
		//强制将缓存区的数据进行输出
		outputStream.flush();
		//关流
		outputStream.close();
		in.close();
	}
}
